package com.ruoyi.project.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.project.domain.Project;
import com.ruoyi.project.domain.ProjectRisk;
import com.ruoyi.project.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 项目Controller
 * 
 * @author zhaogang
 * @date 2024-07-12
 */
@RestController
@RequestMapping("/project/project")
public class ProjectController extends BaseController
{
    @Autowired
    private IProjectService projectService;

    /**
     * 查询项目列表,根据用户显示相应项目
     */
    @PreAuthorize("@ss.hasPermi('project:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(Project project)
    {
        SysUser user = getLoginUser().getUser();
//        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long userId = user.getUserId();
        startPage();
        List<Project> list = projectService.selectProjectListByUserId(userId,project);
        System.out.println("list = ????????????没有值？？？？？" + list);
        return getDataTable(list);
    }
    /**
     * 查询项目列表（根据用户查询）
     */
    @PreAuthorize("@ss.hasPermi('project:project:list')")
    @GetMapping("/listAllByUserId")
    public AjaxResult listAllByUserId() {
        List<Project> list = projectService.selectProjectListAllByUserId();
        return success(list);
    }

    /**
     * 导出项目列表
     */
    @PreAuthorize("@ss.hasPermi('project:project:export')")
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Project project)
    {
        List<Project> list = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        util.exportExcel(response, list, "项目数据");
    }

    /**
     * 获取项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:project:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId)
    {
        return success(projectService.selectProjectByProjectId(projectId));
    }

    /**
     * 新增项目
     */
    @PreAuthorize("@ss.hasPermi('project:project:add')")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Project project)
    {
        /** 项目名称唯一验证 */
        Long id = projectService.selectNameByProject(project);
        System.out.println("id = " + id);
        if(id != null){
            return error("项目名称不能重复!");
        }
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改项目
     */
    @PreAuthorize("@ss.hasPermi('project:project:edit')")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Project project)
    {
        /** 项目名称唯一验证 */
        Long id = projectService.selectNameByProject(project);
        System.out.println("id = " + id);
        if(id != null){
            return error("项目名称不能重复!");
        }
        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除项目
     */
    @PreAuthorize("@ss.hasPermi('project:project:remove')")
    @Log(title = "项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds)
    {
        //删除失败, 此项目被xxx模块使用,不能删除!
        List<Map<String, Object>> list = projectService.checkAll(projectIds[0]);
        if(!list.isEmpty()){
            return error("删除失败, 此项目被" + list.get(0).get("moduleName") + "模块使用,不能删除!");
        }
        return toAjax(projectService.deleteProjectByProjectIds(projectIds));
    }

    @GetMapping("/getCascader")
    public AjaxResult getCascader() {
        List<Map<String, Object>> list = projectService.getCascader();
        if(list.isEmpty()){
            return error("暂无项目");
        }
        System.out.println("list =查级联的返回内容！！！！！！！！！ " + list);
        return success(list);
    }


}
